package notice.util;

import common.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description: 幂等检查
 * @author: zk
 * @since: 2022-08-22 16:12
 */
@Slf4j
@Component
public class IdempotentUtil {
    @Resource
    private RedisUtils redisUtils;
    private static final String IDEMPOTENT_RECORD = "IDEMPOTENT_RECORD:";

    /**
     * 幂等检查
     *
     * @param clz
     * @param key
     * @return true:已经消费过，false:未消费
     */
    public boolean idempotentCheck(Class clz,String key) {
        String idempotentRecord = redisUtils.get(clz,IDEMPOTENT_RECORD+key, String.class);
        return StringUtils.isNotBlank(idempotentRecord);
    }

    /**
     * 添加消费记录
     *
     * @param clz
     * @param key
     */
    public void addIdempotentRecord(Class clz,String key,Long expire) {
        redisUtils.set(clz,IDEMPOTENT_RECORD+key, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()), expire);
    }


}
